package service.impl;

import java.util.List;




import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import db.MyHibernateSessionFactory;
import entity.Students;
import service.StudentsDAO;

public class StudentsDAOImpl implements StudentsDAO {

	@Override
	public List<Students> queryAllStudents() {
		Transaction tx = null;
		List<Students> list = null;
		String hql = "from Students";
		try {
			Session session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
			tx = session.beginTransaction();
			Query query = session.createQuery(hql);
			list = query.list();
			tx.commit();
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			tx.commit();
			return list;
		}
		finally{
			if (null!=tx) {
				tx=null;
			}
		}
	}

	@Override
	public Students queryStudentsBySid(String sid) {
		Transaction tx = null;
		Students students =null;
		try {
			Session session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
			tx = session.beginTransaction();
			students = (Students)session.get(Students.class, sid);
			tx.commit();
			return students;
		} catch (Exception e) {
			e.printStackTrace();
			tx.commit();
			return students;
		}
		finally
		{
			if (tx!=null) {
				tx=null;
			}
		}
	}

	@Override
	public boolean addStudents(Students s) {
		s.setSid(getNewSid());
		Transaction tx = null;
		try {
			Session session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
			tx = session.beginTransaction();
			session.save(s);
			tx.commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			tx.commit();
			return false;
		}
		finally
		{
			if (tx!=null) {
				tx=null;
			}
		}
	}

	@Override
	public boolean updateStudents(Students s) {
		Transaction tx = null;
		try {
			Session session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
			tx = session.beginTransaction();
			session.update(s);
			tx.commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			tx.commit();
			return false;
		}
		finally
		{
			if (tx!=null) {
				tx=null;
			}
		}
	}

	@Override
	public boolean deleteStudents(String sid) {
		Transaction tx = null;
		try {
			Session session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
			tx = session.beginTransaction();
			Students students =(Students)session.get(Students.class, sid);
			session.delete(students);
			tx.commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			tx.commit();
			return false;
		}
		finally
		{
			if (tx!=null) {
				tx=null;
			}
		}
	}
	
	/**
	 * 生成学生的学号
	 * @return
	 */
	private String getNewSid()
	{
		Transaction tx = null;
		String hqlString="";
		String sidString=null;
		try {
			Session session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
			tx = session.beginTransaction();
			//获取当前学生的最大编号
			hqlString = "select max(sid) from Students";
			Query query = session.createQuery(hqlString);
			sidString = (String)query.uniqueResult();
			if (sidString==null||"".equals(sidString)) {
				sidString="S0000001";
			}
			else {
				String temp = sidString.substring(1);
				int i = Integer.parseInt(temp);
				i++;
				temp = String.valueOf(i);
				int len = temp.length();
				for (int j = 0; j < 7-len; j++) {
					temp="0"+temp;
				}
				sidString="S"+temp;
			}
			tx.commit();
			return sidString;
		} catch (Exception e) {
			e.printStackTrace();
			tx.commit();
			return sidString;
		}
		finally{
			if (null!=tx) {
				tx=null;
			}
		}
		
	}
	
}
